/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun<iot25@lingyun>
 *                  All rights reserved.
 *
 *       Filename:  rlimit.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/03/25)
 *         Author:  LingYun <iot25@lingyun>
 *      ChangeLog:  1, Release initial version on "07/03/25 15:44:48"
 *                 
 ********************************************************************************/

#include <stdio.h> 
#include <string.h>
#include <errno.h>
#include <sys/resource.h> 

void print_limits(char* name, int resource)
{
	struct rlimit limit; 
	
	if(getrlimit(resource, &limit) <0)
	{ 
		printf("getrlimit for %s failure: %s\n", strerror(errno)); 
		return ;
	} 
	printf("%-15s ",name); 
	
	if(limit.rlim_cur == RLIM_INFINITY)
	{ 
		printf("(infinite) "); 
	}
	else
	{ 
		printf("%-15ld",limit.rlim_cur); 
	}
	
	if(limit.rlim_max == RLIM_INFINITY)
	{ 
		printf("(infinite) "); 
	}else
	{ 
		printf("%-15ld",limit.rlim_max); 
	} 
	printf("\n"); 
} 
int main(void)
{ 
	struct rlimit limit = {0};
	print_limits("RLIMIT_NPROC", RLIMIT_NPROC);
	print_limits("RLIMIT_DATA", RLIMIT_DATA);
	print_limits("RLIMIT_STACK", RLIMIT_STACK);
	print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);
	printf("\nAfter set RLIMIT_NOFILE:\n");

	getrlimit(RLIMIT_NOFILE, &limit );
	limit.rlim_cur = limit.rlim_max;
	setrlimit(RLIMIT_NOFILE, &limit );
	
	print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);
	return 0;
}

